package com.situ.springboot.service.impl;

import com.situ.springboot.mapper.UserMapper;
import com.situ.springboot.pojo.User;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.LayUITableResult;
import com.situ.springboot.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    // Service:业务
    @Override
    public LayUITableResult selectByPage(Integer page, Integer limit, User user) {
        // 查询当前页的数据
        // select * from user limit (page-1)*limit,limit
        int offset = (page - 1) * limit;
        List<User> list = userMapper.selectByPage(offset, limit, user);

        // 查询总的数量
        long count = userMapper.selectTotalCount(user);

        return LayUITableResult.ok(list, count);
    }

    @Override
    public void add(User user) {
        user.setPassword(MD5Util.MD5Encode(user.getPassword()) + MD5Util.SALT);
        userMapper.add(user);
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        // delete form user where id in(10,16);
        for (Integer id : ids) {
            deleteById(id);
        }
    }

    @Override
    public User login(String name, String password, Integer type) {
        return userMapper.login(name, MD5Util.MD5Encode(password + MD5Util.SALT), type);
    }
}
